package com.qf.controller;

import com.qf.entity.Coupon;
import com.qf.entity.User;
import com.qf.service.CouponCustomService;
import com.qf.tools.JavaWebToken;
import com.qf.tools.ResponseUtils;
import com.qf.tools.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author 刘珈玮
 * @Date 2020/7/25 11:19
 */
@RestController
@RequestMapping("/couponCustomController")
public class CouponCustomController {
    @Autowired
    private CouponCustomService couponCustomService;

    /**
     *
     * 立即抢购(提交订单) 查询当前用户拥有的优惠券信息接口
     *
     * @param user 对应user实体类 接收token
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/getAllCouponInfoById", method = RequestMethod.POST)
    public ResultEntity getAllCouponInfoById(@RequestBody User user) {
        if (user != null) {
            String token = user.getToken();
            Map<String, Object> stringObjectMap = JavaWebToken.parserJavaWebToken(token);
            Integer userId = (Integer) stringObjectMap.get("userId");
            List<Coupon> allCouponInfoById = couponCustomService.getAllCouponInfoById(userId);
            if (allCouponInfoById != null) {
                return ResultEntity.success(allCouponInfoById);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }
}
